﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CreateParallelPipe : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				UIApplication application = commandData.Application;
                Autodesk.Revit.ApplicationServices.Application application2 = application.Application;
				Document document = application.ActiveUIDocument.Document;
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Selection selection = activeUIDocument.Selection;
				List<ViewType> viewTypes = YJKRevitTools.ListViewNeed(2);
				if (!YJKRevitTools.ViewCheck(activeUIDocument, viewTypes))
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					transaction = new Transaction(document, "创建平行管道");
					HYCreatePipeHelper hycreatePipeHelper = new HYCreatePipeHelper();
					List<string> listPipeType = new List<string>();
					listPipeType = HYCreatePipeHelper.GetElementType(document, typeof(PipeType));
					List<string> listPipingSystemType = new List<string>();
					listPipingSystemType = HYCreatePipeHelper.GetElementType(document, typeof(PipingSystemType));
					ISelectionFilter selectionFilter = new HoriPipeFilter();
					for (;;)
					{
						transaction.Start();
						try
						{
							Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "请选择创建平行管道的依据管道");
							Element element = document.GetElement(reference);
							document.ActiveView.HYSetVisibility(element.Category, true);
							Pipe pipe = (Pipe)element;
							if (new FormDrawParallelPipe(listPipeType, listPipingSystemType, pipe, document).ShowDialog() != DialogResult.OK)
							{
								break;
							}
							foreach (drawPipeStruct drawPipeStruct in FormDrawParallelPipe.structPipe)
							{
								PipeType pipeTypeAccordName = HYCreatePipeHelper.GetPipeTypeAccordName(document, drawPipeStruct.pipeTypeName);
								hycreatePipeHelper.DrawParallelPipe(document, drawPipeStruct.offSet, (drawPipeStruct.levelOffSet * 1000.0).ToString(), pipe, pipeTypeAccordName, drawPipeStruct.pipeDiameter.ToString(), HYCreatePipeHelper.GetPipingSystemTypeAccordName(document, drawPipeStruct.pipingSystemName));
							}
							document.Regenerate();
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException)
						{
							break;
						}
						FormDrawParallelPipe.structPipe.Clear();
						transaction.Commit();
					}
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}

		private bool JustPipeSystemIsVisiable(Document doc, string szPipeSystem)
		{
            Autodesk.Revit.DB.View activeView = doc.ActiveView;
			bool flag = true;
			IEnumerable<ElementId> filters = activeView.GetFilters();
			List<Element> list = new List<Element>();
			foreach (ElementId elementId in filters)
			{
				list.Add(doc.GetElementById(elementId));
			}
			foreach (Element element in list)
			{
				if (element == null)
				{
					break;
				}
				flag = activeView.GetFilterVisibility(element.Id);
				if (!flag)
				{
					ParameterFilterElement parameterFilterElement = element as ParameterFilterElement;
					parameterFilterElement.GetCategories();
					ICollection<ElementId> ruleParameters = parameterFilterElement.GetRuleParameters();
					ICollection<FilterRule> rules = parameterFilterElement.GetRules();
					if (ruleParameters.Count == rules.Count)
					{
						IEnumerator<ElementId> enumerator3 = ruleParameters.GetEnumerator();
						enumerator3.Reset();
						foreach (FilterRule filterRule in rules)
						{
							enumerator3.MoveNext();
							int integerValue = enumerator3.Current.IntegerValue;
							doc.GetElement(enumerator3.Current);
							if (!(integerValue.ToString() != "RBS_PIPING_SYSTEM_TYPE_PARAM") && filterRule is FilterElementIdRule)
							{
								FilterElementIdRule filterElementIdRule = filterRule as FilterElementIdRule;
								Element element2 = doc.GetElement(filterElementIdRule.RuleValue);
								MessageBox.Show(element2.Name);
								if (element2.Name == szPipeSystem)
								{
									flag = false;
									break;
								}
								flag = true;
							}
						}
						if (!flag)
						{
							break;
						}
					}
				}
			}
			MessageBox.Show(szPipeSystem + ":kejianxing" + flag.ToString());
			return flag;
		}
	}
}
